package com.zk.iotgateway.manager;

import com.zk.iotgateway.netty.server.ServerForTerminal;
import com.zk.iotgateway.rabbitmq.PublisherConfig;
import com.zk.iotgateway.service.IServerService;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * 全局管理类
 * @author:mn.m
 * @create 2019-04-19 下午 03:20
 **/
public class GlobalManager {

    private static GlobalManager instance;
    /*
    *  iserver
    * */
    private final Map<String, IServerService> serviceMap=new ConcurrentHashMap<String, IServerService>();

    /*
    * 保存设备id
    *
    * */
    public static ConcurrentHashMap<Channel,String> DEVMANAGER;

    /*
    * 网关服务缓存
    * Key 为port
    * value 为 开启的服务端
    * */
    public  static  ConcurrentHashMap<String, ServerForTerminal> SERVERMANAGER;
    /*
    *
    *  分发消息队列 容器
    * */
    public  static ConcurrentHashMap<String, Map<String, PublisherConfig>> MQMANAGER;



    static {
        DEVMANAGER =new ConcurrentHashMap<>();
        SERVERMANAGER =new ConcurrentHashMap<>();
        MQMANAGER =new ConcurrentHashMap<>();
    }

    public static GlobalManager getInstance()
    {
        if(instance==null) {
            instance=new GlobalManager();
        }
        return instance;
    }

    public final void registerService(String servicePort, IServerService service) {
        serviceMap.put(servicePort, service);
    }

    public final IServerService getService(String servicePort)
    {
        return serviceMap.get(servicePort);
    }

    public final IServerService removeService(String servicePort)
    {
        return serviceMap.remove(servicePort);
    }

    public final int getSize(){
        return serviceMap.size();
    }


}
